package com.topsail.demo.xxx.facade;

import com.google.common.collect.Lists;
import com.topsail.demo.xxx.api.Rest;
import com.topsail.demo.xxx.api.dto.BrandDTO;
import com.topsail.demo.xxx.api.facade.BrandFacade;
import com.topsail.demo.yyy.api.facade.UserFacade;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.shenyu.client.apidocs.annotations.ApiDoc;
import org.apache.shenyu.client.apidocs.annotations.ApiModule;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <B>主类名称：</B>BrandFacade<BR>
 * <B>概要说明：</B>专注于业务逻辑<BR>
 *
 * @author steven.zhoulin
 * 
 * @since 2022年09月18日 9:50
 */
@Slf4j
@DubboService
@ApiModule(value = "BrandFacade")
public class BrandFacadeImpl implements BrandFacade {

    @DubboReference(check = false)
    private UserFacade userFacade;

    private DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    @ShenyuDubboClient(path = "listBrand")
    @ApiDoc(desc = "listBrand")
    public Rest listBrand() {
        List<String> objects = Lists.newArrayList();
        objects.add("VIVO");
        objects.add("OPPO");
        objects.add("OnePlus");
        return Rest.success(objects);
    }

    @Override
    @ShenyuDubboClient(path = "listBrandDTO")
    @ApiDoc(desc = "listBrandDTO")
    public Rest listBrandDTO() {

        List<BrandDTO> list = new ArrayList<>();


        list.add(BrandDTO.builder()
                .brandCode(1000)
                .brandName("APPLE")
                .brandDesc("APPLE")
                .createTime(df.format(LocalDateTime.now()))
                .updateTime(df.format(LocalDateTime.now()))
                .build());

        list.add(BrandDTO.builder()
                .brandCode(1001)
                .brandName("OPPO")
                .brandDesc("OPPO")
                .createTime(df.format(LocalDateTime.now()))
                .updateTime(df.format(LocalDateTime.now()))
                .build());

        return Rest.success(list);
    }

    @Override
    @ShenyuDubboClient(path = "/sayHi")
    @ApiDoc(desc = "sayHi")
    public Rest sayHi(String name) {
        return Rest.success("hello: " + name);
    }

    @Override
    @ShenyuDubboClient(path = "uuid")
    @ApiDoc(desc = "uuid")
    public Rest uuid() {
        String crmToken = RpcContext.getServerAttachment().getAttachment("crmToken");
        log.info("crmToken: {}", crmToken);

        String uuid = userFacade.getUserName(1L);
        log.info("uuid: {}", uuid);
        return Rest.success(uuid);
    }

    @Override
    @ShenyuDubboClient(path = "findByListId")
    @ApiDoc(desc = "findByListId")
    public Rest findByListId(final List<Long> ids) {
        log.info("ids: {}", ids);
        List<Long> list = ids.stream().map(e -> e * 10).collect(Collectors.toList());
        log.info("list: {}", list);
        return Rest.success(list);
    }

    @Override
    @ShenyuDubboClient("/batchSave")
    @ApiDoc(desc = "batchSave")
    public Rest batchSave(final List<BrandDTO> brandDTOList) {
        for (BrandDTO brandDTO : brandDTOList) {
            log.info("brandDTO: {}", brandDTO);
        }
        return Rest.success();
    }

}
